package com.ruoyi.jiaopei.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.jiaopei.domain.vo.*;
import com.ruoyi.jiaopei.mapper.TbClassMapper;
import com.ruoyi.jiaopei.mapper.TbKjMapper;
import com.ruoyi.jiaopei.mapper.TbSortCurriculumMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jiaopei.domain.bo.TbKjStudentBo;
import com.ruoyi.jiaopei.domain.TbKjStudent;
import com.ruoyi.jiaopei.mapper.TbKjStudentMapper;
import com.ruoyi.jiaopei.service.ITbKjStudentService;

import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * 课节学员Service业务层处理
 *
 * @author macy
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class TbKjStudentServiceImpl implements ITbKjStudentService {

    private final TbKjStudentMapper baseMapper;
    private final TbClassMapper classMapper;
    private final TbKjMapper kjMapper;
    private final TbSortCurriculumMapper sortCurriculumMapper;

    /**
     * 查询课节学员
     */
    @Override
    public TbKjStudentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public StudentKsVo queryKs(TbKjStudentBo bo) {
        return baseMapper.queryKs(bo);
    }

    @Override
    public Long selectCount(Wrapper<TbKjStudent> queryWrapper){
        return  baseMapper.selectCount(queryWrapper);
    }


    /**
     * 查询课节学员列表
     */
    @Override
    public TableDataInfo<TbKjStudentVo> queryPageList(TbKjStudentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbKjStudent> lqw = buildQueryWrapper(bo);
        Page<TbKjStudentVo> result = baseMapper.selectPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询课节学员列表
     */
    @Override
    public List<TbKjStudentVo> queryList(TbKjStudentBo bo) {
        LambdaQueryWrapper<TbKjStudent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbKjStudent> buildQueryWrapper(TbKjStudentBo bo) {

        QueryWrapper<TbKjStudent> lqw1 = Wrappers.query();
        lqw1.eq(StringUtils.isNotBlank(bo.getClassName()),"c.name", bo.getClassName());
        lqw1.eq(StringUtils.isNotBlank(bo.getKjName()),"m.name", bo.getKjName());
        lqw1.eq(StringUtils.isNotBlank(bo.getStudentName()),"s.name", bo.getStudentName());

        LambdaQueryWrapper<TbKjStudent> lqw =lqw1.lambda();

        lqw.eq(bo.getClassId() != null, TbKjStudent::getClassId, bo.getClassId());
        lqw.eq(bo.getKjId() != null, TbKjStudent::getKjId, bo.getKjId());
        lqw.eq(bo.getStudentId() != null, TbKjStudent::getStudentId, bo.getStudentId());
        lqw.eq(bo.getLessonHours() != null, TbKjStudent::getLessonHours, bo.getLessonHours());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbKjStudent::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), TbKjStudent::getPhoto, bo.getPhoto());
        lqw.eq(bo.getSignDate() != null, TbKjStudent::getSignDate, bo.getSignDate());

        return lqw;
    }

    /**
     * 新增课节学员
     */
    @Override
    public Boolean insertByBo(TbKjStudentBo bo) {
        TbKjStudent add = BeanUtil.toBean(bo, TbKjStudent.class);
        validEntityBeforeSave(add);

        if(!clickEntitySave(add)){
            return false;
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertUiByBo(TbKjStudentBo bo) {
        TbKjStudent add = BeanUtil.toBean(bo, TbKjStudent.class);
        validEntityBeforeSave(add);

        if(!clickEntitySave(add)){
            throw new ServiceException("课时不足");
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课节学员
     */
    @Override
    public Boolean updateByBo(TbKjStudentBo bo) {
        TbKjStudent update = BeanUtil.toBean(bo, TbKjStudent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbKjStudent entity){
        if(null==entity.getId()){
           entity.setCreatedDate(new Date());
           entity.setStatus("1");

           TbKjVo kjVo=kjMapper.selectVoById(entity.getKjId());
           if(null!=kjVo){
              TbSortCurriculumVo sortCurriculumVo=sortCurriculumMapper.selectVoById(kjVo.getSortCurriculumId());
              if(null!=sortCurriculumVo){
                  entity.setLessonHours(sortCurriculumVo.getLessonHours());
              }
           }
        }
    }


    /**
     * 保存前 检查 课时是否充足
     * @param entity
     * @return
     */
    private Boolean clickEntitySave(TbKjStudent entity){

        TbClassVo classVo=classMapper.selectVoById(entity.getClassId());
        if(null==classVo){
            throw new ServiceException("班级不存在，无法检查课时");
        }

        TbKjStudentBo bo=new TbKjStudentBo();
        bo.setStudentId(entity.getStudentId());
        bo.setCurriculumId(classVo.getCurriculumId());
        StudentKsVo studentKsVo =baseMapper.queryKs(bo);
        if(null!=studentKsVo&&studentKsVo.getYsc().doubleValue()<studentKsVo.getLessonHours().doubleValue()){
            return true;
        }
        return false;
    }


    /**
     * 批量删除课节学员
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
